var CommonBehavior = require("../CommonBehavior");
Component({
	behaviors: [CommonBehavior],
	properties: {
		from:{
			type:String,
			value:''
		},
		imageIndex:{
			type:String,
			value:''
		},
    urls:{
      type:Array,
      value: []
    }
	},
  relations: {
    "../../wxParser": {
      type: 'ancestor',
      linked: function (target) {

      },
      linkChange: function (target) {

      },
      unlinked: function (target) {

      }
    }
  },
	methods:{
    wxParseImageTap:function(e){
      let that = this;
      let nowImgUrl = e.target.dataset.src;
      let tagFrom = e.target.dataset.from;
      if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) {
          wx.previewImage({
            current: nowImgUrl,// 当前显示图片的http链接
            urls: that.data.urls,// 需要预览的图片http链接列表
            fail:function(e){
              console.log("Preview Image Fail",e);
            } 
          });
      }
    },
    wxParseImageLoad:function(e){
      console.log("wxParseImageLoad");
      let { src } = e.target.dataset;
      if(!src) return;
      let { width, height } = e.detail;//图片的宽高
      let recal = this._wxAutoImageCalculate(width, height);//重新计算图片的宽高
      let { imageheight, imageWidth } = recal;
      let { padding = 0, mode } = this.data.attr;//图片的边距，模式
      let { styleStr = ""} = this.data.styleStr;//图片的样式
      let imageHeightStyle = mode === 'widthFix' ? '' : `height: ${imageheight}px; !important`;//图片的高度
      let newStyleStr = `${styleStr ? styleStr + ';' : ''}${imageHeightStyle ? imageHeightStyle + ';' : ''} width: ${imageWidth}px; padding: 0 ${+padding}px;`;//图片的新样式
      this.setData({
         styleStr: newStyleStr
      }); 
    },
    wxParseImageError:function(e){
      console.log("wxParseImageError");
      console.log(e);
    },
    //计算视觉优先的图片宽度
    _wxAutoImageCalculate:function(originalWidth,originalHeight){
      // 获取图片的原始长宽
      let { padding } = this.data.attr;//获取图片的间距
      let windowWidth = this.data.windowWidth - (2 * padding);
      let results = {};
      // 判断按照那种方式进行缩放
      if (originalWidth > windowWidth) {
        // 在图片width大于手机屏幕width时候
        results.imageWidth = windowWidth;
        results.imageheight = windowWidth * (originalHeight / originalWidth);
      } else {
        // 否则展示原来的数据
        results.imageWidth = originalWidth;
        results.imageheight = originalHeight;
      }
      return results;
    }
	}
});